from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.sql import func

from database import Base

"""
primary_key
unique=True 开启唯一性校验
index=True 创建索引，从而加快查询速度
nullable=True 允许可为空
default=  设置默认值
ForeignKey('users.id') 外键
"""


class User(Base):
    __tablename__ = "users"
    __table_args__ = {'comment': '用户管理表'}

    id = Column(Integer, primary_key=True, index=True, comment="主键，自增")
    username = Column(String(32), unique=True, index=True, nullable=False, comment="用户名")
    hashed_password = Column(String(128), nullable=False, comment="密码")
    create_time = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    last_login_time = Column(DateTime(timezone=True), onupdate=func.now(), comment="上次登录时间")
    login_count = Column(Integer, default=0, comment="登录次数")
    bio = Column(String(256), nullable=True, comment="个人简介")
    last_password_change = Column(DateTime(timezone=True), nullable=True, comment="上次修改密码时间")
    avatar = Column(String(256), default="/media/avatars/default.jpeg", nullable=True, comment="头像")
    last_login_ip = Column(String(64), nullable=True, comment="用户ip")
    role = Column(String(32), default="user", nullable=True, comment="用户角色（如 admin/user/）")

    def __repr__(self):
        return f"<RequestLog {self.id} {self.username} {self.hashed_password} @ {self.create_time}>"
